package com.dly.blog.queue;

import com.dly.blog.service.FreeFrequencyService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBlockingDeque;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
@AllArgsConstructor
@Slf4j
public class CodeQueueRunner implements CommandLineRunner {

    private final RBlockingDeque<String> codeBlockingFairQueue;

    private final FreeFrequencyService freeFrequencyService;

    @Override
    public void run(String... args) throws Exception {
        new Thread(() -> {
            log.info("code 的死信队列 开启中...");
            while (true) {
                String code;
                try {
                    code = codeBlockingFairQueue.take();
                } catch (Exception e) {
                    continue;
                }
                log.info("兑换码 {} 已经失效", code);
                freeFrequencyService.overdue(code);
            }
        }).start();
    }
}
